package com.whmnx.chitchat.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.DynamicAdapter;
import com.whmnx.chitchat.adapter.UnMsgAdpter;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.DynaminBean;
import com.whmnx.chitchat.bean.UnMsgBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : huang
 * date   : 2020/11/27 0027 14:59
 * version: 1.0
 */
public class ALlMsgActivity extends BaseActivity {
    @BindView(R.id.rl)
    RecyclerView rl;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    private int page = 1;
    private int Rows = 10;
    private List<UnMsgBean.ForumListBean> forumList = new ArrayList<>();
    private UnMsgAdpter unMsgAdpter;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.all_msg_activity);
        setTitle("消息");
        refresh.setOnRefreshListener(refreshLayout -> getForumMessageList(true));
        refresh.setOnLoadMoreListener(refreshLayout -> getForumMessageList(false));
    }

    @Override
    protected void loadData() {
        rl.setLayoutManager(new LinearLayoutManager(this));
        rl.setItemAnimator(null);
        unMsgAdpter = new UnMsgAdpter(this);
        rl.setAdapter(unMsgAdpter);

        getForumMessageList(true);
        getUpdateForumMessageState();

        unMsgAdpter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {   //FabuFriendsActivity
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                startActivity(new Intent(ALlMsgActivity.this,FabuFriendsActivity.class).putExtra("ForumMsg_ID", unMsgAdpter.getItem(i).getForum_ID()));
            }
        });
    }

    private void getUpdateForumMessageState() {
        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        HttpClient.get(this, Constants.GETUPDATEFORUMMESSAGESTATE, params,new CallBack<DynaminBean>() {
            @Override
            public void onSuccess(DynaminBean result) {
                MessageEvent messageEvent = new MessageEvent();
                messageEvent.setMessageKey(EventConstants.FANBU);
                EventBus.getDefault().post(messageEvent);
            }
        });
    }

    private void getForumMessageList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        params.put("Type", 99 + "");
        params.put("State", 0 + "");
        params.put("UserID", SPUtil.getString(Constants.USERID));
        HttpClient.get(this, Constants.GETFORUMMESSAGELIST, params, new CallBack<UnMsgBean>() {
            @Override
            public void onSuccess(UnMsgBean result) {
                if (isRefresh){
                    forumList.clear();
                    forumList.addAll(result.getForumList());
                    refresh.finishRefresh(true);
                    unMsgAdpter.replaceAll(forumList, true);
                }else {
                    if (result.getForumList().size() > 0) {
                        forumList.addAll(result.getForumList());
                        unMsgAdpter.addAll(forumList);
                        refresh.finishLoadMore(true);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });
    }
}
